<template>
  <div class="room-action">
    <!-- 输入任意文本 -->
    <van-field
      class="input"
      v-model="text"
      placeholder="问医生"
      @keyup.enter="sendText"
    />
    <van-uploader :preview-image="false" :after-read="sendImage">
      <icon class="cp-icon" name="consult-img" />
    </van-uploader>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { UploaderAfterRead } from 'vant/lib/uploader/types'
import { Toast } from 'vant'
import { ApiRes } from '@/types/data'
import { Image } from '@/types/consult'
import request from '@/utils/request'
const emits = defineEmits<{
  (e: 'sendText', value: string): void
  (e: 'sendImage', value: Image): void
}>()
const text = ref('')
const sendText = () => {
  emits('sendText', text.value)
  text.value = ''
}
const sendImage: UploaderAfterRead = async data => {
  if (Array.isArray(data)) return
  if (!data.file) return
  const t = Toast.loading('正在上传')
  const formData = new FormData()
  formData.append('file', data.file)
  const res = await request.post<ApiRes<Image>>('upload', formData)
  t.clear()
  emits('sendImage', res.data.data)
}
</script>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;
  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }
  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>
